using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using MediaPortal.Util;
using MediaPortal.GUI.Library;
using MediaPortal.GUI.Video;
using c3o.Framework.Web;

namespace MediaPortal.GUI
{
	public class GUIHelper
	{
        public static void SendFeedBack()
        {
            

        }



        public static string FormatSize(ulong SizeInBytes, uint Decimals)
        {
            double d = 0;

            string suffix = string.Empty;

            if (SizeInBytes < (1ul << 10)) return SizeInBytes.ToString() + " Bytes";                      // byte

            else if (SizeInBytes < (1ul << 20)) { d = SizeInBytes / (double)(1ul << 10); suffix = "KB"; } // kilobyte

            else if (SizeInBytes < (1ul << 30)) { d = SizeInBytes / (double)(1ul << 20); suffix = "MB"; } // megabyte

            else if (SizeInBytes < (1ul << 40)) { d = SizeInBytes / (double)(1ul << 30); suffix = "GB"; } // gigabyte

            else if (SizeInBytes < (1ul << 50)) { d = SizeInBytes / (double)(1ul << 40); suffix = "TB"; } // terabyte

            else if (SizeInBytes < (1ul << 60)) { d = SizeInBytes / (double)(1ul << 50); suffix = "PB"; } // petabyte

            else { d = SizeInBytes / (double)(1ul << 60); suffix = "EB"; }                                // exabyte

            return String.Format("{0:F" + Decimals.ToString() + "} ", d) + suffix;
        }


		public static void Databind(DataTable dt, GUIFacadeControl guiControl)
		{
			//GUIPropertyManager.SetProperty("#selecteditem", "Loading ...");
			GUIWindowManager.Process();

            int i = 0;

            // Must clear control is rows are less than items in control
            if (dt.Rows.Count < guiControl.Count)
            {
                GUIControl.ClearControl(guiControl.WindowId, guiControl.GetID);
            }

			foreach (DataRow row in dt.Rows)
			{
                GUIListItem item = null;

                if (i < guiControl.Count)
                {
                    item = guiControl[i];
                }
                else
                {
                    item = new GUIListItem();
                }

				//GUIListItem item = new GUIListItem();

				item.IsFolder = false;
				item.IsRemote = false;
				item.IconImage = "defaultVideo.png";

				if (dt.Columns.Contains("IsFolder"))
				{
					item.IsFolder = System.Convert.ToBoolean(row["IsFolder"].ToString());
				}

				if (dt.Columns.Contains("IconImage"))
				{
					item.IconImage = row["IconImage"].ToString();
				}

				if (dt.Columns.Contains("Path"))
				{
					item.Path = row["Path"].ToString();
				}
			
				if (dt.Columns.Contains("Label"))
				{
					item.Label = row["Label"].ToString();
				}

				if (dt.Columns.Contains("Label2"))
				{
					item.Label2 = row["Label2"].ToString();
				}

				if (dt.Columns.Contains("Label3"))
				{
					item.Label3 = row["Label3"].ToString();
				}

				if (dt.Columns.Contains("DVDLabel"))
				{
					item.DVDLabel = row["DVDLabel"].ToString();
				}

                if (i >= guiControl.Count)
                {
                    guiControl.Add(item);
                }
				
                i++;                
			}

            // Refresh list so you see changes
            GUIControl.RefreshControl(guiControl.WindowId, guiControl.GetID);
		}

		protected void DownloadAndCache(string imageUrl, string title)
		{
			if (imageUrl.Length > 0)
			{
				string coverArtImage = MediaPortal.Util.Utils.GetCoverArtName(Thumbs.MovieTitle, title);
				string largeCoverArtImage = MediaPortal.Util.Utils.GetLargeCoverArtName(Thumbs.MovieTitle, title);

				if (!System.IO.File.Exists(coverArtImage))
				{
					string imageExtension;
					imageExtension = System.IO.Path.GetExtension(imageUrl);
					if (imageExtension.Length > 0)
					{
						string temporaryFilename = "temp";
						temporaryFilename += imageExtension;
						MediaPortal.Util.Utils.FileDelete(temporaryFilename);

						MediaPortal.Util.Utils.DownLoadAndCacheImage(imageUrl, temporaryFilename);
						if (System.IO.File.Exists(temporaryFilename))
						{
							MediaPortal.Util.Picture.CreateThumbnail(temporaryFilename, coverArtImage, 128, 128, 0);
							MediaPortal.Util.Picture.CreateThumbnail(temporaryFilename, largeCoverArtImage, 512, 512, 0);
						}

						MediaPortal.Util.Utils.FileDelete(temporaryFilename);
					}
				}
			}
		}
	}
}
